package com.lxmes.business.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.NcWorkInstructionManagementMapper;
import com.lxmes.business.domain.NcWorkInstructionManagement;
import com.lxmes.business.service.INcWorkInstructionManagementService;

/**
 * 作业指导书管理Service业务层处理
 * 
 * @author lx
 * @date 2023-06-01
 */
@Service
public class NcWorkInstructionManagementServiceImpl implements INcWorkInstructionManagementService 
{
    @Autowired
    private NcWorkInstructionManagementMapper ncWorkInstructionManagementMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询作业指导书管理
     * 
     * @param id 作业指导书管理主键
     * @return 作业指导书管理
     */
    @Override
    public NcWorkInstructionManagement selectNcWorkInstructionManagementById(Long id)
    {
        return ncWorkInstructionManagementMapper.selectNcWorkInstructionManagementById(id);
    }

    /**
     * 查询作业指导书管理列表
     * 
     * @param ncWorkInstructionManagement 作业指导书管理
     * @return 作业指导书管理
     */
    @Override
    public List<NcWorkInstructionManagement> selectNcWorkInstructionManagementList(NcWorkInstructionManagement ncWorkInstructionManagement)
    {
        return ncWorkInstructionManagementMapper.selectNcWorkInstructionManagementList(ncWorkInstructionManagement);
    }

    /**
     * 新增作业指导书管理
     * 
     * @param ncWorkInstructionManagement 作业指导书管理
     * @return 结果
     */
    @Override
    public int insertNcWorkInstructionManagement(NcWorkInstructionManagement ncWorkInstructionManagement)
    {
        ncWorkInstructionManagement.setCreateTime(DateUtils.getNowDate());
        return ncWorkInstructionManagementMapper.insertNcWorkInstructionManagement(ncWorkInstructionManagement);
    }

    /**
     * 修改作业指导书管理
     * 
     * @param ncWorkInstructionManagement 作业指导书管理
     * @return 结果
     */
    @Override
    public int updateNcWorkInstructionManagement(NcWorkInstructionManagement ncWorkInstructionManagement)
    {
        ncWorkInstructionManagement.setUpdateTime(DateUtils.getNowDate());
        return ncWorkInstructionManagementMapper.updateNcWorkInstructionManagement(ncWorkInstructionManagement);
    }

    /**
     * 批量删除作业指导书管理
     * 
     * @param ids 需要删除的作业指导书管理主键
     * @return 结果
     */
    @Override
    public int deleteNcWorkInstructionManagementByIds(Long[] ids)
    {
        return ncWorkInstructionManagementMapper.deleteNcWorkInstructionManagementByIds(ids);
    }

    /**
     * 删除作业指导书管理信息
     * 
     * @param id 作业指导书管理主键
     * @return 结果
     */
    @Override
    public int deleteNcWorkInstructionManagementById(Long id)
    {
        return ncWorkInstructionManagementMapper.deleteNcWorkInstructionManagementById(id);
    }
    @Override
    public String imporIteminfo(List<NcWorkInstructionManagement> ncWorkInstructionManagementList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncWorkInstructionManagementList) || ncWorkInstructionManagementList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcWorkInstructionManagement ncWorkInstructionManagement : ncWorkInstructionManagementList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncWorkInstructionManagement);
                ncWorkInstructionManagement.setCreateBy(operName);
                ncWorkInstructionManagement.setUpdateBy(operName);
                this.insertNcWorkInstructionManagement(ncWorkInstructionManagement);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
